﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HRM.Utility
{
    public static class ExtensionMethods
    {
        public static int ToInt32(this string str)
        {
            int ret;
            if (Int32.TryParse(str, out ret))
                return ret;
            else
                return 0;
        }

        public static float ToFloat(this string str)
        {
            float ret;
            if (float.TryParse(str, out ret))
                return ret;
            return 0;
        }

        public static DateTime ToDateTime(this string str)
        {
            DateTime ret;
            if (DateTime.TryParse(str, out ret))
                return ret;
            else
                return new DateTime(1900, 1, 1);
        }

        public static DateTime ToDateTime(this string str, string format)
        {
            System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
            dateInfo.ShortDatePattern = format;
            try
            {
                DateTime date = DateTime.Parse(str, dateInfo);
                return date;
            }
            catch (Exception ex)
            {
                return new DateTime(1900, 1, 1);
            }
        }

        public static bool ToBool(this string str)
        {
            bool ret = false;
            if(bool.TryParse(str, out ret))
            {
                return ret;
            }
            return false;
        }

        public static bool ToBool(this int value)
        {
            if (value == 0)
                return false;
            return true;
        }

        public static int ToInt32(this object obj)
        {
            int ret;
            if(Int32.TryParse(obj.ToString(),out ret))
            {
                return ret;
            }
            return 0;
        }
    }
}
